<?php 
	class ControllerMemoryViewreminder extends Controller {
		private $error = array();
		public function index() {
			$this->document->addStyle('admin/view/stylesheet/bootstrap-datetimepicker.min.css');
			$this->document->addScript('admin/view/javascript/bootstrap-datetimepicker.js');
			$this->document->addScript('admin/view/javascript/bootstrap-datetimepicker.zh-CN.js');
			$this->document->setTitle("viewreminder");
			$this->load->model("knowledge/knowledge");
			$htoday = date("Y年m月d日");
			$today = date("Y-m-d");//"2017-03-01";//
			$starttime = $today." 00:00:01";
			$endtime = $today." 23:59:59";
			$data['maincontent'] = "今天是".$htoday."<br>要复习的知识点详情如下";
			//主体表格数据
			if (isset($this->request->get['page'])) {
				$page = $g_args['page'] = $this->request->get['page'];
			}else{
				$g_args['page'] = 1;
			}
			if (isset($this->request->get['sort'])) {
				$g_args['sort'] = $this->request->get['sort'];
			} else {
				$g_args['sort'] = 'date';
			}
			if (isset($this->request->get['order'])) {
				$g_args['order'] = $this->request->get['order'];
			} else {
				$g_args['order'] = 'ASC';
			}
			if(isset($this->request->get['kname'])) {
				$g_args['kname'] = $this->request->get['kname'];
			}
			if(isset($this->request->get['label'])) {
				$g_args['label'] = $this->request->get['label'];
			}
			if(isset($this->request->get['times'])) {
				$g_args['times'] = $this->request->get['times'];
			}
			if(isset($this->request->get['star'])) {
				$g_args['star'] = $this->request->get['star'];
			}
			if(isset($this->request->get['todaystarttime'])) {
				$g_args['todaystarttime'] = $this->request->get['todaystarttime'];
			}else{
				$g_args['todaystarttime'] = $starttime;
			}
			if(isset($this->request->get['todayendtime'])) {
				$g_args['todayendtime'] = $this->request->get['todayendtime'];
			}else{
				$g_args['todayendtime'] = $endtime;
			}
			$all = $this->getKnowledgeListForView($g_args);
			$data['knowledges'] = $all['knowledges'];
			$data['pagination'] = $all['pagination'];
            if($this->request->server['REQUEST_METHOD'] == 'POST') {
                if (isset($this->request->post['page'])) {
					$page = $g_args['page'] = $this->request->post['page'];
				}else{
					$g_args['page'] = 1;
				}
				if (isset($this->request->post['sort'])) {
					$g_args['sort'] = $this->request->post['sort'];
				} else {
					$g_args['sort'] = 'date';
				}
				if (isset($this->request->post['order'])) {
					$g_args['order'] = $this->request->post['order'];
				} else {
					$g_args['order'] = 'ASC';
				}
				if(isset($this->request->post['kname'])) {
					$g_args['kname'] = $this->request->post['kname'];
				}
				if(isset($this->request->post['label'])) {
					$g_args['label'] = $this->request->post['label'];
				}
				if(isset($this->request->post['star'])) {
					$g_args['star'] = $this->request->post['star'];
				}
				if(isset($this->request->post['times'])) {
					$g_args['times'] = $this->request->post['times'];
				}
				if(isset($this->request->post['todaystarttime'])) {
					$g_args['todaystarttime'] = $this->request->post['todaystarttime'];
				}else{
					$g_args['todaystarttime'] = $starttime;
				}
				if(isset($this->request->post['todayendtime'])) {
					$g_args['todayendtime'] = $this->request->post['todayendtime'];
				}else{
					$g_args['todayendtime'] = $endtime;
				}
				$all = $this->getKnowledgeListForView($g_args);
				//$all = $this->model_knowledge_knowledge->getKnowledgesByUidForView($g_args);
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($all,0));
            }
            $data['selectstarurl'] = $this->url->link('memory/viewreminder/selectstar','',true)."&token=".$this->session->data['token'];
			$data['header'] = $this->load->controller('common/headerin');
	 		$data['footerin'] = $this->load->controller('common/footerin');
	 		$data['footer'] = $this->load->controller('common/footer');
	 		$this->response->setOutput($this->load->view('memory/viewreminder', $data));
		}
		public function selectstar() {
            if($this->request->server['REQUEST_METHOD'] == 'POST') {
                $this->load->model('complate/complate');
                $args = array(
                	"star" =>$this->request->post['star'],
                	"cid"  =>$this->request->post['cid']
                	);
                 $num=$this->model_complate_complate->selectStar($args);
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($num,0));
            }
		}
		public function getKnowledgeListForView($args){
			//主体数据
			$this->load->model('knowledge/knowledge');
			$data = array();
			$filter_data = array(
				'sort'      => $args['sort'],
				'order'     => $args['order'],
				'kname'     => $args['kname'],
				'label'     => $args['label'],
				'star'      => $args['star'],
				'times'      => $args['times'],
				'todaystarttime' => $args['todaystarttime'],
				'todayendtime'   => $args['todayendtime'],
				'user_id'   => $this->session->data['user_id'],
				'start'     => ((int)$args['page'] -1)*10,
				'limit'     => 10
			);
			$results = $this->model_knowledge_knowledge->getKnowledgesByUidForView($filter_data);
			foreach ($results['maindata'] as $result) {
				$data[] = array(
						'kid'     => $result['id'],
						'cid'     => $result['cid'],
						'kname'   => $result['kname'],
						'kdetail' => $result['kdetail'],
						'times'	  => $result['times'],
						'date' 	  => $result['date'],
						'star'    => $result['star']
					); 
			}
			//构造分页的url
			$url = '';
			if(isset($args['sort'])) {
				$url .= '&sort='.$args['sort'];
			}
			if(isset($args['order'])) {
				$url .= '&order='.$args['order'];
			}
			if(isset($args['kname'])) {
				$url .= '&kname='.$args['kname'];
			}
			if(isset($args['label'])) {
				$url .= '&label='.urlencode($args['label']);
			}
			if(isset($args['star'])) {
				$url .= '&star='.$args['star'];
			}
			if(isset($args['todaystarttime'])) {
				$url .= '&starttime='.urlencode($args['todaystarttime']);
			}
			if(isset($args['todayendtime'])) {
				$url .= '&todayendtime='.urlencode($args['todayendtime']);
			}
			//分页开始
			$pagination = new Pagination();
			$pagination->total = $results['total'];
			$pagination->page  = $args['page'];
			$pagination->limit = 10;
			$pagination->url = $this->url->link('memory/viewreminder', 'token=' . $this->session->data['token'] . $url.'&page={page}', true);
			$pagination = $pagination->render();
			//分页结束
			$info = array(
					'knowledges' => $data,
					'pagination' => $pagination
				);
			return  $info;//$filter_data;
		}
	}
 ?>